package com.hnichr.crm.biz.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.hnichr.crm.biz.customer.api.CrmCustomerApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;
import java.util.HashMap;

import javax.crypto.Cipher;
/**
 * @author Zhangww
 * @date 2023/5/17 1:42
 */

public class RSAUtils {

    public static final String ENCRYPT_TYPE = "RSA";
    private static final Logger logger = LoggerFactory.getLogger(RSAUtils.class);

    /**
     * 公钥加密
     * @param content 要加密的内容
     * @param publicKey 公钥
     */
    public static String encrypt(String content, String publicKey) {
        try{
            RSA rsa=new RSA(null,publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param content 要解密的内容
     * @param privateKey 私钥
     */
    public static String decrypt(String content, String privateKey) {
        try {
            RSA rsa=new RSA(privateKey,null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成公钥私钥
     */
    public static void main(String[] args) {
        try {
            KeyPair pair= SecureUtil.generateKeyPair(ENCRYPT_TYPE);
            PrivateKey privateKey=pair.getPrivate();
            PublicKey publicKey=pair.getPublic();
            // 获取 公钥和私钥 的 编码格式（通过该 编码格式 可以反过来 生成公钥和私钥对象）
            byte[] pubEncBytes=publicKey.getEncoded();
            byte[] priEncBytes=privateKey.getEncoded();
            // 把公钥和私钥的编码格式转换为Base64文本
            String pubEncBase64= Base64.encode(pubEncBytes);
            String priEncBase64=Base64.encode(priEncBytes);
            logger.info("公钥: {}", pubEncBase64);
            logger.info("私钥: {}", priEncBase64);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @desc: 将字符串转换成RSAPublicKey类型
     * @date 2020-6-12 11:03:05
     * @param
     * @return
     */
    public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) throws Exception {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
        RSAPublicKey publicKey = null;
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        try {
            publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
        } catch (InvalidKeySpecException var4) {
            logger.error("getRSAPublidKeyBybase64 error",var4);
        }
        return publicKey;
    }

    /**
     * @desc: 将字符串转换成RSAPrivateKey类型
     * @date 2020-6-12 11:03:01
     * @param
     * @return
     */
    public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) throws Exception{
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
        RSAPrivateKey privateKey = null;
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        try {
            privateKey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec);
        } catch (InvalidKeySpecException var4) {
            logger.error("getRSAPrivateKeyBybase64 error",var4);
        }
        return privateKey;
    }

}

